/**
 * 
 */
package com.easidea.tim.dao.system.orgnization;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springside.core.dao.extend.HibernateEntityExtendDao;

import com.easidea.tim.entity.system.orgnization.Staff;
import com.easidea.tim.entity.system.orgnization.StaffCreditLimit;



/**
 * @author Harrison
 *
 */
public class StaffCreditLimitDAO extends HibernateEntityExtendDao<StaffCreditLimit>{
  /**
   * 初始化员工授信额度
   * @param staff
   * @param initCreditLimit
   * @param creator
   * @param createId
   * @return
   */
	public String initStaffCreditLimit(Staff staff, 
		 double initCreditLimit,String creator, long createId) {

		StaffCreditLimit limit = new StaffCreditLimit();			
		limit.setCreateTime(new Date());
		limit.setCreator(creator);
		limit.setDeleted((short)0);
		limit.setModifier(creator);
		limit.setModifyTime(new Date());
		limit.setInitCreditLimit(initCreditLimit);
		limit.setCurrentCreditLimit(initCreditLimit);
		limit.setStaff(staff);
		this.save(limit);
		return "success";	 
 }
	/**
	 * 根据业务员id获取业务员额度信息
	 * @param staffId
	 * @return
	 */
	public StaffCreditLimit getStaffCreditLimitByStaffId(long staffId) {
	   String hql ="from " + StaffCreditLimit.class.getName() + " where deleted = 0 and staff.id = ? ";
	   Query q = this.createQuery(hql, staffId);
		return (StaffCreditLimit)q.uniqueResult();
	}	
	/**
	 * 调整员工授信额度
	 * @param staffId
	 * @param currentLimit
	 * @param modifylimit
	 * @param modifier
	 * @param modifyId
	 * @return
	 */
	public String modifyStaffCreditLimit(long staffId,double currentLimit,
			double modifylimit, String modifier, long modifyId) {
		StaffCreditLimit limit = getStaffCreditLimitByStaffId(staffId);
		limit.setPreCreditLimit(currentLimit);
		limit.setModifier(modifier);
		limit.setModifyTime(new Date());
		limit.setCurrentCreditLimit(modifylimit);
		limit.setModifyLimitStaffId(modifyId);
		limit.setModifyLimitTime(new Date());
		return "success";
	}
	/**
	 * 扣除员工额度
	 * @param staffId
	 * @param deductLimit
	 * @param modifier
	 * @param modifyId
	 * @return
	 */
	public String deductStaffCreditLimit(long staffId,double deductLimit,
				String modifier, long modifyId) {
		StaffCreditLimit limit = getStaffCreditLimitByStaffId(staffId);
		limit.setPreCreditLimit(limit.getCurrentCreditLimit());
		limit.setModifier(modifier);
		limit.setModifyTime(new Date());
		limit.setCurrentCreditLimit((limit.getCurrentCreditLimit()- deductLimit));
		limit.setModifyLimitStaffId(modifyId);
		limit.setModifyLimitTime(new Date());
		return "success";
	}
	 /**
	 * 判断业务员当前授信额度是否足够扣除当前额度
	 * @param staffId
	 * @param deductLimit
	 * @return
	 */
	public boolean creditLimitIsEnough(long staffId, double deductLimit) {
		StaffCreditLimit limit = getStaffCreditLimitByStaffId(staffId);
		if ((limit.getCurrentCreditLimit() -  deductLimit) >= 0) {
			return true;
		}
		return false; 
		
	}
	
	/**
	 * @see com.easidea.tim.service.system.orgnization.StaffService#getStaffCurrentCreditInfo(Long)
	 */
	public Map<String, Double> getStaffCurrentCreditInfo(Long staffId) {
		Map<String, Double> rtnMap = null;
		if(staffId != null){
			rtnMap = new HashMap<String, Double>();
			StaffCreditLimit scl = getStaffCreditLimitByStaffId(staffId);
			if(scl != null && scl.getCurrentCreditLimit() != null){
				rtnMap.put(StaffCreditLimit.CREDIT_LIMITATION_STAFF_CURRENT_VALUE, scl.getCurrentCreditLimit());
			}
			Double salesCustomerDebtAmount = getSalesCustomerDebtAmount(staffId);
			if(salesCustomerDebtAmount != null){
				rtnMap.put(StaffCreditLimit.CREDIT_LIMITATION_STAFF_CUSTOMER_VALUE, salesCustomerDebtAmount);
			}else{
				rtnMap.put(StaffCreditLimit.CREDIT_LIMITATION_STAFF_CUSTOMER_VALUE, 0D);
			}
		}
		return rtnMap;
	}
	
	/**
	 * 根据员工提取对应的客户，汇总客户当前的欠款信息，排除欠款为负数的数据
	 * @param staffId
	 * @return
	 */
	private Double getSalesCustomerDebtAmount(Long staffId){
		final String sql = "SELECT SUM(cust.account_summary)" +
				" FROM tim_customer_customer cust, tim_orgnization_staff st" +
				" WHERE cust.staff_id = st.id AND cust.deleted = 0 AND st.deleted = 0" +
				" AND cust.account_summary > 0 AND st.id = " + staffId;
		return (Double) getHibernateTemplate().execute(new HibernateCallback() {
		
			public Double doInHibernate(Session session) throws HibernateException,
					SQLException {
				SQLQuery query = session.createSQLQuery(sql);
				Object result = query.uniqueResult();
				if (result!=null){
					return ((BigDecimal)result).doubleValue();
				}else{
					return null;
				}
			}
		});
	}  
}
